import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/entrance_guard/api.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/utils/index.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class AddEntranceGuard extends StatefulWidget {
  const AddEntranceGuard({super.key});

  @override
  State<StatefulWidget> createState() => _AddEntranceGuardState();

}

class _AddEntranceGuardState extends State<AddEntranceGuard> {

  final _params = {
    "faceLockName": "",
    "faceLockMac": "",
    "faceLockSn": ""
  };

  bool checkIsNext () {
    return Utils.isNotEmpty(_params["faceLockName"])
        && Utils.isNotEmpty(_params["faceLockMac"])
        && Utils.isNotEmpty(_params["faceLockSn"]);
  }

  Future<void> _save() async {
    try {
      Map resData = await entranceGuardApi.saveEntranceGuard(_params);
      print(resData);
      if (resData["code"] == 200) {
        EasyLoading.showToast("添加成功");
        Stacker.pop(true);
      }
    } catch (error) {
      print(error);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text("添加智能电表", style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimaryDark,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: ListView(
        children: [
          MenuBlock(
            borderRadius: const BorderRadius.all(Radius.circular(0)),
            items: [
              MenuBlockItem(
                  prefix: const Text("*", style: TextStyle(color: ThemeVars.colorError),),
                  text: "门禁名称",
                  showArrow: false,
                  suffix: TextField(
                    textAlign: TextAlign.right,
                    decoration: const InputDecoration(
                      border: InputBorder.none,
                      hintText: "请输入",
                      hintStyle: TextStyle(color: ThemeVars.colorTextTer, fontWeight: FontWeight.w400),
                      isDense: true
                    ),
                    onChanged: (value) {
                      setState(() {
                        _params["faceLockName"] = value;
                      });
                    },
                  )
              ),
              MenuBlockItem(
                  prefix: const Text("*", style: TextStyle(color: ThemeVars.colorError),),
                  text: "门禁MAC码",
                  showArrow: false,
                  suffix: TextField(
                    textAlign: TextAlign.right,
                    decoration: const InputDecoration(
                      border: InputBorder.none,
                      hintText: "请输入MAC编码",
                      hintStyle: TextStyle(color: ThemeVars.colorTextTer, fontWeight: FontWeight.w400),
                      isDense: true
                    ),
                    onChanged: (value) {
                      setState(() {
                        _params["faceLockMac"] = value;
                      });
                    },
                  )
              ),
              MenuBlockItem(
                  prefix: const Text("*", style: TextStyle(color: ThemeVars.colorError),),
                  text: "门禁SN码",
                  showArrow: false,
                  suffix: TextField(
                    textAlign: TextAlign.right,
                    decoration: const InputDecoration(
                      border: InputBorder.none,
                      hintText: "请输入SN编码",
                      hintStyle: TextStyle(color: ThemeVars.colorTextTer, fontWeight: FontWeight.w400),
                      isDense: true
                    ),
                    onChanged: (value) {
                      setState(() {
                        _params["faceLockSn"] = value;
                      });
                    },
                  )
              ),
            ],
          ),
          Padding(
            padding: const EdgeInsets.only(left: ThemeVars.paddingPage, right: ThemeVars.paddingPage, top: 60),
            child: CupertinoButton.filled(
              padding: const EdgeInsets.symmetric(vertical: 10),
              disabledColor: ThemeVars.colorDisabled,
              onPressed: checkIsNext() ? () async {
                await _save();
              } : null,
              child: const Text("下一步"),
            ),
          ),
        ],
      ),
    );
  }

}